<template>
  <div>
    <AModal
      v-bind="$attrs"
      @close="modalEvent('close')"
      @ok="modalEvent('ok')"
      @open="modalEvent('open')"
      @cancel="modalEvent('cancel')"
    >
      <AForm ref="formRef" :model="form" auto-label-width>
        <AFormItem
          field="customerName"
          label="客户名称"
          :rules="[{ required: true, message: '请输入客户名称' }]"
          :validate-trigger="['change', 'input', 'blur']"
        >
          <AInput v-model="form.customerName" placeholder="客户名称"> </AInput>
        </AFormItem>
        <AFormItem field="customerAddress" label="地址">
          <AInput v-model="form.customerAddress" placeholder="地址" />
        </AFormItem>
        <AFormItem field="taxNo" label="税号">
          <AInput v-model="form.taxNo" placeholder="税号" />
        </AFormItem>
        <AFormItem field="bankAccount" label="银行账户">
          <AInput v-model="form.bankAccount" placeholder="银行账户" />
        </AFormItem>
        <AFormItem field="payeeAcctBank" label="开户行">
          <AInput v-model="form.payeeAcctBank" placeholder="开户行" />
        </AFormItem>
        <AFormItem field="contactName" label="联系人">
          <AInput v-model="form.contactName" placeholder="联系人" />
        </AFormItem>
        <AFormItem field="contactPhone" label="电话">
          <AInput v-model="form.contactPhone" placeholder="电话" />
        </AFormItem>
        <AFormItem field="remark" label="备注">
          <ATextarea v-model="form.remark" placeholder="备注"></ATextarea>
        </AFormItem>
      </AForm>
    </AModal>
  </div>
</template>

<script lang="ts" setup>
import { Message } from '@arco-design/web-vue'
import { editCustomer, CustomerReq, CustomerResItem } from '@renderer/api/company'
import { useMenuState } from '@renderer/store'
import { deleteNullObject } from '@renderer/utils/data-processing'

interface Props {
  [key: string]: any
  checkData?: Partial<CustomerResItem>
}
const props = withDefaults(defineProps<Props>(), {
  checkData: () => {
    return {}
  }
})
const emit = defineEmits(['update:visible', 'reload'])
const attrs = useAttrs()
const formRef = ref()
const form = ref<CustomerReq>({
  customerId: undefined,
  customerName: '',
  customerAddress: '',
  taxNo: '',
  bankAccount: '',
  payeeAcctBank: '',
  contactName: '',
  contactPhone: '',
  remark: ''
})
const menuParam = useMenuState()

// 弹窗事件
const modalEvent = (type: string) => {
  switch (type) {
    case 'ok':
      formRef.value.validate((errors) => {
        console.log(errors)
        if (!errors) {
          // 新增
          if ((attrs.title as string).includes('新增')) {
            form.value.customerId = undefined
          }
          editCustomer(deleteNullObject(form.value)).then(() => {
            Message.success((attrs.title as string).includes('新增') ? '新增成功' : '修改成功')
            modalEvent('cancel')
            emit('reload')
          })
        }
      })
      break
    case 'close':
      menuParam.switchEditStatus(false)
      formRef.value.resetFields()
      emit('update:visible', false)
      break
    case 'open':
      Object.assign(form.value, props.checkData)
      menuParam.switchEditStatus(true)
      break
    case 'cancel':
      formRef.value.resetFields()
      emit('update:visible', false)
      menuParam.switchEditStatus(false)
      break
    default:
      break
  }
}
</script>
<style scoped></style>
